package com.ruiyangwang.ListenBook.repository;

import com.ruiyangwang.ListenBook.dataObject.TrackDo;
import com.ruiyangwang.ListenBook.util.DButil;
import com.ruiyangwang.ListenBook.util.Log;
import com.ruiyangwang.ListenBook.viewObject.StudioTrackListView;
import lombok.SneakyThrows;

import java.io.InputStream;
import java.lang.reflect.Parameter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author syzygy
 * @date 2022/07/11 19:50
 **/
public class TrackRepo {
    @SneakyThrows
    public void insert(Integer uid, String title, String type, InputStream content) throws SQLException {
        String sql = "insert into tracks (uid,title,type,content) values(?,?,?,?)";
        try(Connection c = DButil.connection()){
            try (PreparedStatement ps = c.prepareStatement(sql)){
                ps.setInt(1,uid);
                ps.setString(2,title);
                ps.setString(3,type);
                ps.setBinaryStream(4,content);
                Log.println("执行sql语句：" + ps.toString());
                ps.executeUpdate();
            }
        }
    }
    @SneakyThrows
    public int  selectCountByUid(Integer uid) {
        String sql = "select count(*) from tracks where uid = ?";
        try(Connection c = DButil.connection()){
            try(PreparedStatement ps = c.prepareStatement(sql)){
                ps.setInt(1,uid);
                Log.println("执行sql:" + ps);
                try(ResultSet res = ps.executeQuery()){
                    res.next();
                    Log.println(res.getInt(1));
                    return res.getInt(1);
                }
            }
        }
    }
    @SneakyThrows
    public List<TrackDo> selectListByUidLimitOffset(Integer uid, int countParPage, int offset) {
        List<TrackDo> list =new ArrayList<>();
        String sql = "select tid,uid,title,type from tracks where uid = ? order by tid desc limit ? offset ?";
        try(Connection c = DButil.connection()){
            try(PreparedStatement ps = c.prepareStatement(sql)){
                ps.setInt(1,uid);
                ps.setInt(2,countParPage);
                ps.setInt(3,offset);
                Log.println("打印sql : " + ps);
                try(ResultSet rs = ps.executeQuery()){
                    while(rs.next()){
                        TrackDo trackDo =new TrackDo(
                            rs.getInt(1),
                            rs.getInt(2),
                            rs.getString(3),
                            rs.getString(4)
                        );
                        list.add(trackDo);
                    }
                }
            }
        }
        return list;

    }
    @SneakyThrows
    public List<TrackDo> selectListByTidList(List<Integer> tidList) {
        String s = String.join(", " ,tidList.stream().map(String::valueOf).collect(Collectors.toList()));
        String sql = String.format("select tid,uid,title,type from tracks where tid in (%s) order by tid",s);
        List<TrackDo> list = new ArrayList<>();
        try(Connection c = DButil.connection()){
            try(PreparedStatement ps = c.prepareStatement(sql)){
                Log.println("执行sql ：" + ps);
                try(ResultSet rs = ps.executeQuery()){
                    while(rs.next()){
                        list.add( new TrackDo(rs.getInt("tid"),rs.getInt("uid"),rs.getString("title"),rs.getString("type")));
                    }
                    return list;
                }
            }
        }

    }
}